package com.bw.ogin.view


import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.common.mvi.BaseMviActivity
import com.bw.common.path.RouthPath
import com.bw.ogin.R

import com.bw.ogin.databinding.ActivityLoginBinding
import com.bw.ogin.entity.LoginValues
import com.bw.ogin.intent.LoginViewIntent
import com.bw.ogin.state.LoginUiState
import com.bw.ogin.vm.LoginViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = RouthPath.PATH_LOGIN_PAGE)
@AndroidEntryPoint
class LoginActivity : BaseMviActivity<ActivityLoginBinding>() {

    private val mViewModel:LoginViewModel by viewModels()

    override fun getLayoutId(): Int = R.layout.activity_login

    override fun initViews() {
        //获取记住的账户和密码
        var preAccount = MMKV.defaultMMKV().decodeString("pre_account","")
        var prePwd = MMKV.defaultMMKV().decodeString("pre_pwd","")
        if(!preAccount.isNullOrEmpty() && !prePwd.isNullOrEmpty()){
            mDataBind.includeLogin.cbRememberPwd.isChecked = true
            mDataBind.includeLogin.edtLoginName.setText(preAccount)
            mDataBind.includeLogin.edtLoginPwd.setText(prePwd)
        }
        //注册账号按钮点击事件
        mDataBind.includeLogin.tvRegister.setOnClickListener {
            mDataBind.cltLoginCard.visibility = View.INVISIBLE
            mDataBind.cltRegisterCard.visibility = View.VISIBLE
            mDataBind.llUserAgreement.visibility = View.INVISIBLE

        }
        //登录按钮点击事件
        mDataBind.includeLogin.btnLogin.setOnClickListener{
//            var uName = mDataBind.includeLogin.edtLoginName.text.toString().trim()
//            var uPwd = mDataBind.includeLogin.edtLoginPwd.text.toString().trim()
//            if(!mDataBind.cbAgree.isChecked){
//                showToastMsg("请先同意用户协议")
//            }else{
//                loginToServer(uName,uPwd)
//            }
            ARouter.getInstance().build("/new/activity").navigation()

        }
        //注册按钮点击事件
        mDataBind.includeRegister.btnRegister.setOnClickListener{
            var uName = mDataBind.includeRegister.edtRegisterName.text.toString().trim()
            var uPwd = mDataBind.includeRegister.edtRegisterPwd.text.toString().trim()
            var uRepwd = mDataBind.includeRegister.edtRegisterRepwd.text.toString().trim()
            if(!uName.isNullOrEmpty() && !uPwd.isNullOrEmpty() &&!uRepwd.isNullOrEmpty() &&uPwd ==uRepwd){
                registerToServer(uName,uPwd)
            }
        }
    }

    private fun registerToServer(uName: String, uPwd: String) {
        lifecycleScope.launch {
            mViewModel.channel.send(LoginViewIntent.RegisterToServer(uName,uPwd))
        }
    }

    private fun loginToServer(uName: String, uPwd: String) {
        lifecycleScope.launch {
            mViewModel.channel.send(LoginViewIntent.LoginToServer(uName,uPwd))
        }
    }



    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is LoginUiState.LoginSuccess-> handLoginSuccess(it.data)
                is LoginUiState.RegisterSuccess-> handRegisterSuccess(it.data)
                is LoginUiState.LoginFail-> showToastMsg(it.msg)
                is LoginUiState.RegisterFail-> showToastMsg(it.msg)
                else ->{}
            }
        }
    }

    /**
     * 注册成功
     */
    private fun handRegisterSuccess(data: Any) {
        var entity =data as LoginValues
        //回显新的账户和密码
        mDataBind.includeLogin.edtLoginName.setText(entity.uName)
        mDataBind.includeLogin.edtLoginPwd.setText(entity.uPwd)
        //提示登录成功
        showToastMsg("注册成功")
        //切换到登录页面
        mDataBind.cltLoginCard.visibility=View.VISIBLE
        mDataBind.cltRegisterCard.visibility = View.INVISIBLE
        mDataBind.llUserAgreement.visibility = View.VISIBLE

    }

    /**
     * 登录成功
     */
    private fun handLoginSuccess(data: Any) {
        var entity =data as LoginValues
        //是否需要记住密码
        if(mDataBind.includeLogin.cbRememberPwd.isChecked){
            MMKV.defaultMMKV().encode("pre_account",entity.uName)
            MMKV.defaultMMKV().encode("pre_pwd",entity.uPwd)
        }
        showToastMsg("登陆成功")

        //跳转到主页面
        ARouter.getInstance().build(RouthPath.PATH_MAIN_PAGE).navigation()

        //使用局部降级策略，此时全局降级策略将不会执行

        this@LoginActivity.finish()
    }

}